package abc.database;

import abc.base.AbcBaseController;
import abc.base.BaseController;
import abc.base.BaseDataExecuor;
import abc.base.BaseModel;
import abc.config.DatasourceConfigure;
import abc.model.ElExpressionInfo;
import abc.utils.AssertUtil;
import abc.utils.JacksonUtil;
import abc.utils.ReflectionUtil;
import abc.utils.StringUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.cursor.Cursor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.SqlSession;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Component
public class DataExecuor extends BaseDataExecuor {

    public DataExecuor(DatasourceConfigure datasourceConfigure) {
        super(datasourceConfigure);
    }

    /**
     * 主键值
     */
    public <T extends BaseModel> Serializable pkVal(Class<T> modelClass) {
        TableInfo tableInfo = TableInfoHelper.getTableInfo(modelClass);
        return (Serializable) tableInfo.getPropertyValue(this, tableInfo.getKeyProperty());
    }

    public Map upset(String dbname, String table, List<BaseModel> modelList) throws SQLException {
        return upset(dbname, table, modelList, getSqlSession(true));
    }

    public Map upset(String dbname, String table, List<BaseModel> modelList, SqlSession session) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isEmpty(modelList, "Data array can not be empty!!!");
        Map result = new HashMap();
        try {
            for (BaseModel model : modelList) {
                boolean boolVal = false;
                Serializable pk = pkVal(model.getClass());
                if(StringUtils.checkValNull(pk) || !exists(dbname, table, model)) {
                    MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.INSERT_ONE);
                    boolVal = SqlHelper.retBool(session.insert(mappedStatement.getId(), model));
                }
                else {
                    MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.UPSERT_ONE);
                    boolVal = SqlHelper.retBool(session.update(mappedStatement.getId(), model));
                }
                if(boolVal) {
                    associativeUpdate(dbname, table, model, session);
                }
                result.put(model.getId(), boolVal);
            }
            commitSqlSession(session);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result;
    }

    public Map insert(String dbname, String table, List<BaseModel> modelList) throws SQLException {
        return insert(dbname, table, modelList, getSqlSession(true));
    }

    public Map insert(String dbname, String table, List<BaseModel> modelList, SqlSession session) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isEmpty(modelList, "Data array can not be empty!!!");
        Map result = new HashMap();
        try {
            for (BaseModel model : modelList) {
                MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.INSERT_ONE);
                boolean boolVal = SqlHelper.retBool(session.insert(mappedStatement.getId(), model));
                if(boolVal) {
                    associativeUpdate(dbname, table, model, session);
                }
                result.put(model.getId(), boolVal);
            }
            commitSqlSession(session);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result;
    }

    public Boolean update(String dbname, String table, BaseModel model, Long id) throws SQLException {
        return update(dbname, table, model, id, getSqlSession(true));
    }

    public Boolean update(String dbname, String table, BaseModel model, Long id, SqlSession session) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(id<=0, "Id can not be less than or equal to zero!!!");
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.ENTITY, getModelById(dbname, table, model, id));
        boolean result = false;
        try {
            MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.UPDATE_BY_ID);
            result = SqlHelper.retBool(session.update(mappedStatement.getId(), map));
            if(result) {
                associativeUpdate(dbname, table, model, session);
            }
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
        return result;
    }

    public Boolean update(String dbname, String table, BaseModel model, BaseModel queryModel) throws SQLException {
        return update(dbname, table, model, queryModel, getSqlSession(true));
    }

    public Boolean update(String dbname, String table, BaseModel model, BaseModel queryModel, SqlSession session) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(queryModel==null, "Query model can not be null!!!");
        Map<String, Object> mapParams = CollectionUtils.newHashMapWithExpectedSize(1);
        mapParams.put(Constants.ENTITY, getModelById(dbname, table, model, Long.valueOf(0)));
        mapParams.put(Constants.WRAPPER_ENTITY, elExpressionQueryHelper.getDefaultQueryWrapper(queryModel));

        boolean result = false;
        try {
            MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.UPDATE);
            result = SqlHelper.retBool(session.update(mappedStatement.getId(), mapParams));
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
        return result;
    }

    public Boolean delete(String dbname, String table, BaseModel model, Long id) throws SQLException {
        return delete(dbname, table, model, id, getSqlSession(true));
    }

    public Boolean delete(String dbname, String table, BaseModel model, Long id, SqlSession session) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(id<=0, "Id can not be less than or equal to zero!!!");
        boolean result = false;
        try {
            MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.DELETE_BY_ID);
            if(StringUtil.isNotEmpty(logicDeleteFiled) && hasField(dbname, table, logicDeleteFiled)) {
                ReflectionUtil.setFieldValue(model, StringUtil.getHumpName(logicDeleteFiled, true), logicDeleteValue);
                mappedStatement = model.getMappedStatement(SqlMethod.LOGIC_DELETE_BY_ID);
                result = SqlHelper.retBool(session.update(mappedStatement.getId(), id));
            }
            else {
                result = SqlHelper.retBool(session.delete(mappedStatement.getId(), id));
            }
            commitSqlSession(session);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result;
    }

    public Boolean delete(String dbname, String table, BaseModel model, List<Long> ids) throws SQLException {
        return delete(dbname, table, model, ids, getSqlSession(true));
    }

    public Boolean delete(String dbname, String table, BaseModel model, List<Long> ids, SqlSession session) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(ids.size()<1, "Ids can not be empty!!!");
        AssertUtil.isTrue(elExpressionQueryHelper.getPrimaryKeyNames(dbname, table).size()!=1, "Can not find the primary key name!!!");
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.COLLECTION, ids);
        Boolean result = false;
        try {
            MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.DELETE_BATCH_BY_IDS);
            if(StringUtil.isNotEmpty(logicDeleteFiled) && hasField(dbname, table, logicDeleteFiled)) {
                ReflectionUtil.setFieldValue(model, StringUtil.getHumpName(logicDeleteFiled, true), logicDeleteValue);
                mappedStatement = model.getMappedStatement(SqlMethod.LOGIC_DELETE_BATCH_BY_IDS);
                result = SqlHelper.retBool(session.update(mappedStatement.getId(), map));
            }
            else {
                result = SqlHelper.retBool(session.delete(mappedStatement.getId(), map));
            }
            commitSqlSession(session);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result;
    }

    public Boolean delete(String dbname, String table, BaseModel model, Map map) throws SQLException {
        return delete(dbname, table, model, map, getSqlSession(true));
    }

    public Boolean delete(String dbname, String table, BaseModel model, Map map, SqlSession session) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(map.size()>0, "Map size can not be empty!!!");
        Map<String, Object> mapParams = CollectionUtils.newHashMapWithExpectedSize(1);
        mapParams.put(Constants.COLUMN_MAP, map);
        Boolean result = false;
        try {
            MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.DELETE_BY_MAP);
            if(StringUtil.isNotEmpty(logicDeleteFiled) && hasField(dbname, table, logicDeleteFiled)) {
                ReflectionUtil.setFieldValue(model, StringUtil.getHumpName(logicDeleteFiled, true), logicDeleteValue);
                mappedStatement = model.getMappedStatement(SqlMethod.LOGIC_DELETE_BY_MAP);
                result = SqlHelper.retBool(session.update(mappedStatement.getId(), mapParams));
            }
            else {
                result = SqlHelper.retBool(session.delete(mappedStatement.getId(), mapParams));
            }
            commitSqlSession(session);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result;
    }

    public <T> List<T> select(String dbname, String table, BaseModel model, Map map) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(map.size()>0, "Map size can not be empty!!!");
        Map<String, Object> mapParams = CollectionUtils.newHashMapWithExpectedSize(1);
        mapParams.put(Constants.COLUMN_MAP, map);
        List<T> result = new ArrayList<>();
        SqlSession session = getSqlSession(true);
        try {
            MappedStatement mappedStatement = model.getMappedStatement(SqlMethod.SELECT_BY_MAP);
            result = session.selectList(mappedStatement.getId(), mapParams);
            //commitSqlSession(session);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result;
    }

    public <T extends BaseModel> List<? extends BaseModel> select(String dbname, String table, BaseModel queryModel, String... fields) throws SQLException {
        return select(dbname, table, queryModel, queryModel.getClass(), null, fields);
    }

    public <T extends BaseModel> List<? extends BaseModel> select(String dbname, String table, BaseModel queryModel, List<OrderItem> orders, String... fields) throws SQLException {
        return select(dbname, table, queryModel, queryModel.getClass(), orders, fields);
    }

    public <T extends BaseModel, R> List<R> select(String dbname, String table, BaseModel queryModel, Class<R> returnType, List<OrderItem> orders, String... fields) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getDefaultQueryWrapper(queryModel);
        if(fields != null && fields.length > 0) {
            wrapper = elExpressionQueryHelper.getSelectFieldQueryWrapper((QueryWrapper) wrapper, fields);
        }
        wrapper = elExpressionQueryHelper.getOrdersQueryWrapper((QueryWrapper) wrapper, orders, dbname, table);
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        List<T> result = new ArrayList<>();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_LIST);
            result = session.selectList(mappedStatement.getId(), map);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result.stream().map(a-> {
            try {
                if(a.getClass().equals(returnType)) {
                    return (R) a;
                }
                return JacksonUtil.convert(a, returnType);
            } catch (IOException e) {
                log.error(e.getMessage());
                return JacksonUtil.parse("{}", returnType);
            }
        }).collect(Collectors.toList());
    }

    public <T extends BaseModel> T select(String dbname, String table, BaseModel queryModel, Long id) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(id<=0, "Id can not be less than or equal to zero!!!");
        Object result = getModelById(dbname, table, queryModel, id);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_BY_ID);
            result = session.selectOne(mappedStatement.getId(), id);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return (T) result;
    }

    public <T extends BaseModel> List<T> select(String dbname, String table, BaseModel queryModel, List<Long> ids) throws SQLException {
        verifier(dbname, table);
        AssertUtil.isTrue(ids.size()>0, "Ids can not be empty!!!");
        AssertUtil.isTrue(elExpressionQueryHelper.getPrimaryKeyNames(dbname, table).size()!=1, "Can not find the primary key name!!!");
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        Wrapper wrapper = (Wrapper) elExpressionQueryHelper.getDefaultQueryWrapper(queryModel).in(elExpressionQueryHelper.getPrimaryKeyNames(dbname, table).get(0), ids);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_BATCH_BY_IDS);
            return session.selectList(mappedStatement.getId(), map);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
    }

    public boolean exist(String dbname, String table, BaseModel queryModel) throws SQLException {
        return first(dbname, table, queryModel, null)!=null;
    }

    public <T extends BaseModel> long count(String dbname, String table, BaseModel queryModel) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getDefaultQueryWrapper(queryModel);
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_COUNT);
            return SqlHelper.retCount(session.<Long>selectOne(mappedStatement.getId(), map));
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
    }

    public boolean exists(String dbname, String table, BaseModel queryModel) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getDefaultQueryWrapper(queryModel);
        ((QueryWrapper) wrapper).last("limit 1");
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_COUNT);
            return SqlHelper.retBool(session.<Long>selectOne(mappedStatement.getId(), map));
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
    }

    public <T extends BaseModel> T first(String dbname, String table, BaseModel queryModel, List<OrderItem> orders, String... fields) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getDefaultQueryWrapper(queryModel);
        if(fields != null && fields.length > 0) {
            wrapper = elExpressionQueryHelper.getSelectFieldQueryWrapper((QueryWrapper) wrapper, fields);
        }
        ((QueryWrapper) wrapper).last("limit 1");
        wrapper = elExpressionQueryHelper.getOrdersQueryWrapper((QueryWrapper) wrapper, orders, dbname, table);

        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        T result = null;
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_LIST);
            result = session.selectOne(mappedStatement.getId(), map);
            return result;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
    }

    public String avg(String dbname, String table, BaseModel queryModel, String field) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getDefaultQueryWrapper(queryModel);
        wrapper = elExpressionQueryHelper.getSelectFieldQueryWrapper((QueryWrapper) wrapper, String.format("avg(%s) as %s", field, field));
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_LIST);
            queryModel = session.selectOne(mappedStatement.getId(), map);
            Object obj = ReflectionUtil.getFieldValue(queryModel, field);
            String value = (obj==null? "": obj.toString());
            return value;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
    }

    public String min(String dbname, String table, BaseModel queryModel, String field) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getDefaultQueryWrapper(queryModel);
        wrapper = elExpressionQueryHelper.getSelectFieldQueryWrapper((QueryWrapper) wrapper, String.format("min(%s) as %s", field, field));
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_LIST);
            queryModel = session.selectOne(mappedStatement.getId(), map);
            Object obj = ReflectionUtil.getFieldValue(queryModel, field);
            String value = (obj==null? "": obj.toString());
            return value;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
    }

    public String max(String dbname, String table, BaseModel queryModel, String field) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getDefaultQueryWrapper(queryModel);
        wrapper = elExpressionQueryHelper.getSelectFieldQueryWrapper((QueryWrapper) wrapper, String.format("max(%s) as %s", field, field));
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_LIST);
            queryModel = session.selectOne(mappedStatement.getId(), map);
            Object obj = ReflectionUtil.getFieldValue(queryModel, field);
            String value = (obj==null? "": obj.toString());
            return value;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }
    }

    public <T extends BaseModel, P extends IPage<T>> P page(String dbname, String table, BaseModel queryModel, P page) throws SQLException {
        return page(dbname, table, queryModel, page, null, false);
    }

    public <T extends BaseModel, P extends IPage<T>> P page(String dbname, String table, BaseModel queryModel, P page, List<OrderItem> orders) throws SQLException {
        return page(dbname, table, queryModel, page, orders, false);
    }

    public <T extends BaseModel, P extends IPage<T>> P page(String dbname, String table, BaseModel queryModel, P page, List<OrderItem> orders, boolean hasTotalCount) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getOrdersQueryWrapper(elExpressionQueryHelper.getDefaultQueryWrapper(queryModel), orders, dbname, table);
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(2);
        map.put(Constants.WRAPPER, wrapper);
        map.put("page", page);
        SqlSession session = getSqlSession();
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_PAGE);
            page.setRecords(session.selectList(mappedStatement.getId(), map));
            if(hasTotalCount) {
                page.setTotal(count(dbname, table, queryModel));
            }
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return page;
    }

    public <T extends BaseModel> Cursor<T> cursor(String dbname, String table, BaseModel queryModel, List<OrderItem> orders) throws SQLException {
        verifier(dbname, table);
        Wrapper wrapper = elExpressionQueryHelper.getOrdersQueryWrapper(elExpressionQueryHelper.getDefaultQueryWrapper(queryModel), orders, dbname, table);
        Map<String, Object> map = CollectionUtils.newHashMapWithExpectedSize(1);
        map.put(Constants.WRAPPER, wrapper);
        AssertUtil.isTrue(map.size()<=0, "Map size can not be less than or equal to zero!!!");
        SqlSession session = getSqlSession();
        Cursor<T> result = null;
        try {
            MappedStatement mappedStatement = queryModel.getMappedStatement(SqlMethod.SELECT_LIST);
            result = session.selectCursor(mappedStatement.getId(), map);
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw e;
        }
        finally {
            closeSqlSession(session);
        }

        return result;
    }

    public <T extends BaseModel> T getModelById(String dbname, String table, T model, Long id) {
        if(id>0) {
            model = (T) fillPrimaryKeys2Model(dbname, StringUtil.getHumpName(table, true), model, id);
        }
        return model;
    }

    public <T extends BaseModel, S extends Serializable> T getModel(String table) {
        return (T) applicationContext.getBean(StringUtil.getHumpName(table, true) + "Model");
    }

    public <T extends BaseModel, S extends Serializable, C extends AbcBaseController> T getModel(C controller) {
        String table = requestMatchSchemaFilter.getTableName(controller.getClass());
        return (T) applicationContext.getBean(StringUtil.getHumpName(table, true) + "Model");
    }

    public <T extends BaseModel, S extends Serializable> T getModel(Class<? extends AbcBaseController> aClass, S queryData) throws IOException {
        String table = requestMatchSchemaFilter.getTableName(aClass);
        var model = (BaseModel) applicationContext.getBean(table+"Model");
        if(queryData instanceof Map) {
            if(elExpressionQueryHelper.getLocalElExprPatternMap().get() == null) {
                elExpressionQueryHelper.getLocalElExprPatternMap().set(new HashMap());
            }
            else {
                elExpressionQueryHelper.getLocalElExprPatternMap().get().clear();
            }
            List<String> removeFiles = new ArrayList<>();
            ((Map<String, Object>) queryData).entrySet().forEach(entry-> {
                elExpressionQueryHelper.getExpressionPatterns().forEach(pattern -> {
                    if(entry.getValue() == null) {
                        return;
                    }
                    if(pattern.matcher(entry.getValue().toString()).find()){
                        elExpressionQueryHelper.getLocalElExprPatternMap().get().put(entry.getKey(), new ElExpressionInfo() {{
                            setField(entry.getKey());
                            setValue(entry.getValue().toString());
                            setPattern(pattern);
                        }});
                        removeFiles.add(entry.getKey());
                    }
                });
            });
            removeFiles.forEach(a-> ((Map<?, ?>) queryData).remove(a));
            model = JacksonUtil.convert(queryData, model.getClass());
        }
        else {
            model = JacksonUtil.convert(queryData, model.getClass());
        }

        return (T) model;
    }
}
